﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;
using KorbeRTR.Utility;

namespace KorbeRTR
{
    public partial class FormAutoAddSet : Form
    {
        public FormAutoAddSet()
        {
            InitializeComponent();
        }

        private void FormAutoAddSet_Load(object sender, EventArgs e)
        {
            //初始化grid安培
            InitGrid安培(grid安培);
            InitGrid面积(grid面积);

            Update安培(grid安培);
            Update面积(grid面积);
        }

        private void Update安培(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;

            var plc = FormMainWindow.OmronPLC;

            //var list = new OmronData.Type添加AH[]
            //{
            //    plc.P平整剂,
            //    plc.G光泽剂,
            //    plc.S湿润剂,
            //    plc.T铜粉添加CRS,
            //    plc.T铜槽H2SO4
            //};

            //for(int i=0; i<list.Length; i++)
            //{
            //    var row = i + 1;

            //    grid.Cell(row, 2).Text = (list[i].开关.Value != 0) ? "开" : "关";
            //    grid.Cell(row, 3).Text = (list[i].模式.Value != 0) ? "自动" : "手动";

            //    grid.Cell(row, 4).Text = list[i].AH设定.Value.ToString();
            //    grid.Cell(row, 5).Text = list[i].当前AH.Value.ToString();
            //    grid.Cell(row, 6).Text = list[i].累计AH.Value.ToString();
            //    grid.Cell(row, 7).Text = list[i].时间设定.Value.ToString();
            //    grid.Cell(row, 8).Text = list[i].当前时间.Value.ToString();
            //    grid.Cell(row, 9).Text = (list[i].当前状态.Value !=0) ? "ON" : "OFF";
            //    grid.Cell(row, 10).Text = list[i].当前异常状态.Value.ToString();
            //}

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        public void InitGrid安培(FlexCell.Grid grid)
        {
            //Grid1设置
            grid.AutoRedraw = false;
           
            grid.DisplayRowNumber = true;
            // 设置行数/列数
            grid.Rows = 5;
            grid.Cols = 11;

            //这个包围区间要减1 居中
            grid.Range(0, 0, grid.Rows-1, grid.Cols-1).Alignment = FlexCell.AlignmentEnum.CenterCenter;
            // 设置单元格文字
            int col = 0;
            grid.Cell(0, col++).Text = "序号";
            grid.Cell(0, col++).Text = "标签";
            grid.Cell(0, col++).Text = "开关";
            grid.Cell(0, col++).Text = "模式";
            grid.Cell(0, col++).Text = "设定AH";
            grid.Cell(0, col++).Text = "实际AH";
            grid.Cell(0, col++).Text = "累计AH";
            grid.Cell(0, col++).Text = "设定时间";
            grid.Cell(0, col++).Text = "实际时间";
            grid.Cell(0, col++).Text = "状态";
            grid.Cell(0, col++).Text = "异常";

            int row = 1;
            grid.Cell(row++, 1).Text = "平整剂";
            grid.Cell(row++, 1).Text = "光泽剂";
            grid.Cell(row++, 1).Text = "湿润剂";
            grid.Cell(row++, 1).Text = "铜粉添加";//原来的CRS
          

            grid.Column(2).CellType = FlexCell.CellTypeEnum.ComboBox;
            grid.Column(3).CellType = FlexCell.CellTypeEnum.ComboBox;
            grid.Column(4).CellType = FlexCell.CellTypeEnum.Button;
          //  grid.Column(5).CellType = FlexCell.CellTypeEnum.Button;
         //   grid.Column(6).CellType = FlexCell.CellTypeEnum.Button;
            grid.Column(7).CellType = FlexCell.CellTypeEnum.Button;
            grid.Column(9).CellType = FlexCell.CellTypeEnum.ComboBox;

            FlexCell.ComboBox cb = grid.ComboBox(2);
            cb.DropDownFont = new Font("Courier New", 9);
            cb.Items.Add("开");
            cb.Items.Add("关");

            cb = grid.ComboBox(3);
            cb.Items.Add("手动");
            cb.Items.Add("自动");

            cb = grid.ComboBox(9);
            cb.Items.Add("OFF");
            cb.Items.Add("ON");

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        

        private void Update面积(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;

            var plc = FormMainWindow.OmronPLC;

            //var list = new OmronData.Type添加面积[]
            //{
            //    plc.C除油添加剂,
            //    plc.W微蚀添加剂,
            //    plc.Y预浸添加剂,
            //    plc.T褪挂H2SO4,
            //    plc.T褪挂H2O2,
            //    plc.T褪挂安定剂
            //};

            //for (int i = 0; i < list.Length; i++)
            //{
            //    var row = i + 1;

            //    grid.Cell(row, 2).Text = (list[i].开关.Value != 0)? "开" : "关";
            //    grid.Cell(row, 3).Text = (list[i].模式.Value != 0)? "自动" : "手动";

            //    grid.Cell(row, 4).Text = list[i].面积设定.Value.ToString();
            //    grid.Cell(row, 5).Text = list[i].当前面积.Value.ToString();
            //    grid.Cell(row, 6).Text = list[i].累计面积.Value.ToString();
            //    grid.Cell(row, 7).Text = list[i].时间设定.Value.ToString();
            //    grid.Cell(row, 8).Text = list[i].当前时间.Value.ToString();
            //    grid.Cell(row, 9).Text = (list[i].当前状态.Value != 0) ? "ON" : "OFF";
            //    grid.Cell(row, 10).Text = list[i].当前异常状态.Value.ToString();
            //}

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        /// <summary>
        /// 初始化面积
        /// </summary>
        /// <param name="grid"></param>
        public void InitGrid面积(FlexCell.Grid grid)
        {
            //Grid1设置
            grid.AutoRedraw = false;
          
            grid.DisplayRowNumber = true;
            // 设置行数/列数
            grid.Rows = 7;
            grid.Cols = 11;
            grid.Column(1).Width = 110;
            //这个包围区间要减1 居中
            grid.Range(0, 0, grid.Rows - 1, grid.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;
            // 设置单元格文字
            int col = 0;
            grid.Cell(0, col++).Text = "序号";
            grid.Cell(0, col++).Text = "标签";
            grid.Cell(0, col++).Text = "开关";
            grid.Cell(0, col++).Text = "模式";
            grid.Cell(0, col++).Text = "设定";
            grid.Cell(0, col++).Text = "实际";
            grid.Cell(0, col++).Text = "累计";
            grid.Cell(0, col++).Text = "设定时间";
            grid.Cell(0, col++).Text = "实际时间";
            grid.Cell(0, col++).Text = "状态";
            grid.Cell(0, col++).Text = "异常";

            int row = 1;
            grid.Cell(row++, 1).Text = "酸性清洁添加剂";
            grid.Cell(row++, 1).Text = "微蚀添加剂";
            grid.Cell(row++, 1).Text = "预浸添加剂";
            grid.Cell(row++, 1).Text = "夹具剥铜H2SO4";
            grid.Cell(row++, 1).Text = "夹具剥铜H2O2";
            grid.Cell(row++, 1).Text = "夹具剥铜安定剂";
        


            grid.Column(2).CellType = FlexCell.CellTypeEnum.ComboBox;
            grid.Column(3).CellType = FlexCell.CellTypeEnum.ComboBox;
            grid.Column(4).CellType = FlexCell.CellTypeEnum.Button;
            //grid.Column(5).CellType = FlexCell.CellTypeEnum.Button;
            //   grid.Column(6).CellType = FlexCell.CellTypeEnum.Button;
            grid.Column(7).CellType = FlexCell.CellTypeEnum.Button;
            grid.Column(9).CellType = FlexCell.CellTypeEnum.ComboBox;

            FlexCell.ComboBox cb = grid.ComboBox(2);
            cb.DropDownFont = new Font("Courier New", 9);
            cb.Items.Add("开");
            cb.Items.Add("关");

            cb = grid.ComboBox(3);
            cb.Items.Add("手动");
            cb.Items.Add("自动");

            cb = grid.ComboBox(9);
            cb.Items.Add("OFF");
            cb.Items.Add("ON");

            grid.AutoRedraw = true;
            grid.Refresh();
        }

        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }

        private void grid安培_ButtonClick(object Sender, FlexCell.Grid.ButtonClickEventArgs e)
        {
            var plc = FormMainWindow.OmronPLC;
            //平整剂设定AH
            {
                if(e.Row == 1 && e.Col ==4)
                {
                    string name = "平整剂";
                    string type = "设定AH";
                    string unit = "AH";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.P平整剂.AH设定.Value = value;
                        }
                    }
                }
            }

            //平整剂设定时间
            {
                if (e.Row == 1 && e.Col == 7)
                {
                    string name = "平整剂";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.P平整剂.时间设定.Value = value;
                        }
                    }
                }
            }

            //光泽剂设定AH
            {
                if (e.Row == 2 && e.Col == 4)
                {
                    string name = "光泽剂";
                    string type = "设定AH";
                    string unit = "AH";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.G光泽剂.AH设定.Value = value;
                        }
                    }
                }
            }

            //光泽剂设定时间
            {
                if (e.Row == 2 && e.Col == 7)
                {
                    string name = "光泽剂";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.G光泽剂.时间设定.Value = value;
                        }
                    }
                }
            }

            //湿润剂设定AH
            {
                if (e.Row == 3 && e.Col == 4)
                {
                    string name = "湿润剂";
                    string type = "设定AH";
                    string unit = "AH";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.S湿润剂.AH设定.Value = value;
                        }
                    }
                }
            }

            //湿润剂设定时间
            {
                if (e.Row == 3 && e.Col == 7)
                {
                    string name = "湿润剂";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.G光泽剂.时间设定.Value = value;
                        }
                    }
                }
            }

            //铜粉添加设定AH(CRS)
            {
                if (e.Row == 4 && e.Col == 4)
                {
                    string name = "铜粉添加";
                    string type = "设定AH";
                    string unit = "AH";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.T铜粉添加CRS.AH设定.Value = value;
                        }
                    }
                }
            }

        }

        private void btn刷新_Click(object sender, EventArgs e)
        {
            Update安培(grid安培);
            Update面积(grid面积);
        }

        private void grid面积_ButtonClick(object Sender, FlexCell.Grid.ButtonClickEventArgs e)
        {
            var plc = FormMainWindow.OmronPLC;
            //酸性清洁添加剂面积设定
            {
                if (e.Row == 1 && e.Col == 4)
                {
                    string name = "酸性清洁添加剂";
                    string type = "设定";
                    string unit = "ft^2";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.C除油添加剂.面积设定.Value = value;
                        }
                    }
                }
            }

            //酸性清洁添加剂设定时间
            {
                if (e.Row == 1 && e.Col == 7)
                {
                    string name = "酸性清洁添加剂";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.C除油添加剂.时间设定.Value = value;
                        }
                    }
                }
            }

            //微蚀添加剂面积设定
            {
                if (e.Row == 2 && e.Col == 4)
                {
                    string name = "微蚀添加剂";
                    string type = "设定面积";
                    string unit = "ft^2";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.W微蚀添加剂.面积设定.Value = value;
                        }
                    }
                }
            }

            //微蚀添加剂设定时间
            {
                if (e.Row == 2 && e.Col == 7)
                {
                    string name = "微蚀添加剂";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.W微蚀添加剂.时间设定.Value = value;
                        }
                    }
                }
            }

            //预浸添加剂设定面积
            {
                if (e.Row == 3 && e.Col == 4)
                {
                    string name = "预浸添加剂";
                    string type = "设定面积";
                    string unit = "ft^2";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.Y预浸添加剂.面积设定.Value = value;
                        }
                    }
                }
            }

            //预浸添加剂设定时间
            {
                if (e.Row == 3 && e.Col == 7)
                {
                    string name = "预浸添加剂";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.Y预浸添加剂.时间设定.Value = value;
                        }
                    }
                }
            }

            //夹具剥铜H2SO4设定面积
            {
                if (e.Row == 4 && e.Col == 4)
                {
                    string name = "夹具剥铜H2SO4";
                    string type = "设定面积";
                    string unit = "ft^2";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.T褪挂H2SO4.面积设定.Value = value;
                        }
                    }
                }
            }

            //夹具剥铜H2SO4设定时间
            {
                if (e.Row == 4 && e.Col == 7)
                {
                    string name = "夹具剥铜H2SO4";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.T褪挂H2SO4.时间设定.Value = value;
                        }
                    }
                }
            }

            //夹具剥铜H2O2设定面积
            {
                if (e.Row == 5 && e.Col == 4)
                {
                    string name = "夹具剥铜H2O2";
                    string type = "设定面积";
                    string unit = "ft^2";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.T褪挂H2O2.面积设定.Value = value;
                        }
                    }
                }
            }

            //夹具剥铜H2O2设定时间
            {
                if (e.Row == 5 && e.Col == 7)
                {
                    string name = "夹具剥铜H2O2";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.T褪挂H2O2.时间设定.Value = value;
                        }
                    }
                }
            }

            //夹具剥铜安定剂设定面积
            {
                if (e.Row == 6 && e.Col == 4)
                {
                    string name = "夹具剥铜安定剂";
                    string type = "设定面积";
                    string unit = "ft^2";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.T褪挂安定剂.面积设定.Value = value;
                        }
                    }
                }
            }

            //夹具剥铜安定剂设定时间
            {
                if (e.Row == 6 && e.Col == 7)
                {
                    string name = "夹具剥铜安定剂";
                    string type = "设定时间";
                    string unit = "秒";
                    double min = 0;
                    double max = 9999;

                    string msg = string.Format("请输入'{0}'{1}:\n(范围:{2}-{3},单位:{4})", name, type, min, max, unit);

                    bool bl = CheckForm(typeof(FormInfo).Name);
                    if (!bl)
                    {
                        FormInfo form = new FormInfo(msg, min, max);
                        //   formLogin.MdiParent = this;
                        form.ShowDialog();
                        if (form.ok)
                        {
                            var value = form.input.ToDouble();
                            //plc.T褪挂安定剂.时间设定.Value = value;
                        }
                    }
                }
            }


        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
